package org.algorithm.wE1.链表;

import org.algorithm.biubiu.wheel_1.common.ListNode;

/**
 * @Date 2022/2/28
 * @Desc {https://leetcode-cn.com/problems/odd-even-linked-list/submissions/}
 */
public class 奇偶链表 {

    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7};
        ListNode node = ListNode.build(arr);
        System.out.println("ListNode.print(oddEvenList(node)) = " + ListNode.print(oddEvenList(node)));
    }

    public static ListNode oddEvenList(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }

        ListNode evenHead = head.next;
        ListNode odd = head;
        ListNode even = evenHead;
        while (even!=null && even.next!=null){
            odd.next = even.next;
            odd = odd.next;
            even.next = odd.next;
            even = even.next;
        }
        odd.next = evenHead;
        return head;
    }
}
